package com.bp.hearthealth.pulse.lnapps.bp_extension

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.bp.hearthealth.pulse.lnapps.BpApplication

/**
 * 弹出Toast提示。
 *
 * @param duration 显示消息的时间  Either {@link #LENGTH_SHORT} or {@link #LENGTH_LONG}
 */
fun Int.showToast(duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(BpApplication.context, this, duration).show()
}

/**
 * 解析xml布局
 *
 * @param parent 父布局
 * @param attachToRoot 是否依附到父布局
 */
fun Int.inflate(parent: ViewGroup, attachToRoot: Boolean = false): View {
    return LayoutInflater.from(parent.context).inflate(this, parent, attachToRoot)
}

/**
 * 获取转换后的时间样式。
 *
 * @return 处理后的时间样式，示例：06:50
 */
fun Int.conversionVideoDuration(): String {
    val minute = 1 * 60
    val hour = 60 * minute
    val day = 24 * hour

    return when {
        this < day -> {
            String.format("%02d:%02d", this / minute, this % 60)
        }

        else -> {
            String.format("%02d:%02d:%02d", this / hour, (this % hour) / minute, this % 60)
        }
    }
}

/**
 * 清除长按时的toast
 * @param bottomNavigationView 当前BottomNavigationView
 * @param ids                  与配置文件中对应的所有id
 */
fun clearToast(bottomNavigationView: BottomNavigationView, ids: MutableList<Int>) {
    var bottomNavigationMenuView: ViewGroup = (bottomNavigationView.getChildAt(0) as ViewGroup)
    //遍历子View,重写长按点击事件
    for (position in 0 until ids.size) {
        bottomNavigationMenuView.getChildAt(position)
            .findViewById<View>((ids[position] as Int?)!!)
            .setOnLongClickListener { true }
    }
}